/*

This code creates figures

Last edited 3/9/2021 by Katherine Wagner


*/

clear all

gl dataRAW  "C:\Users\User\Dropbox\work_in_progress\carbon_lockin\dataRAW\"
gl dataSTATA  "C:\Users\User\Dropbox\work_in_progress\carbon_lockin\dataSTATA\"
gl results  "C:\Users\User\Dropbox\work_in_progress\carbon_lockin\results\"
gl dataR "C:\Users\User\Dropbox\work_in_progress\carbon_lockin\dataR"

import delimited "$dataRAW/user_provided_data\nber_ces\naics5811.csv", clear
gen pien_n = pien if year == 2011
bys naics: egen pien_2011 = mean(pien_n)
gen pien_dfltr = pien_2011 / pien
keep naics year pien_dfltr
collapse (mean) pien_dfltr , by(year)
ren pien_dfltr pien_avg
tempfile energy_dfltrs
sa `energy_dfltrs', replace


*>-------- raw fuels prices ---------------------------------------------------<

use "${dataSTATA}seds\seds_iv.dta", clear
merge m:1 year using `energy_dfltrs', assert(1 3) keep(3) nogen

* deflate to 2011 dollars
replace efPriceNationalcl = efPriceNationalcl * pien_avg
replace efPriceNationalng = efPriceNationalng * pien_avg
replace efPriceNationalpa = efPriceNationalpa * pien_avg

* collapse
collapse (mean) efPriceNationalcl efPriceNationalng efPriceNationalpa, by(year)
keep if year > 1975

la var efPriceNationalcl "National Coal Price for Elec. Generation (2011 USD per Million BTU)"
la var efPriceNationalng "National Nat. Gas Price for Elec. Generation (2011 USD per Million BTU)"
la var efPriceNationalpa "National Oil Price for Elec. Generation (2011 USD per Million BTU)"

tw (line efPriceNationalcl year, lpattern(longdash) lcolor(navy) msymbol(S)) (line efPriceNationalng year, lpattern(solid) lcolor(cranberry) msymbol(O)) ///
	(line efPriceNationalpa year, lpattern(shortdash) lcolor(green) msymbol(D) mcolor(green)), ///
	graphr(color(white) lwidth(vthick)) yscale(noline) xtit("Year") subtit("") title("") ///
	ytit("Price (2011 $ per Million BTU)") legend(label(1 "Coal") label(2 "Natural Gas") label(3 "Oil") cols(3))
	
graph export "${results}\figures\fuel_prices.pdf", as(pdf) replace

*>------------------ electricity prices ---------------------------------------<

import delimited "${dataRAW}/user_provided_data/seds/pr_all.csv", varnames(1) clear 
keep if msn == "ESICD" & state == "US" // this is "electricity prices in the industrial sector, United States (dollars per million BTU)"

foreach var of varlist v* {
	local yr_lab : variable label `var'
	ren `var' year`yr_lab'
	}


replace msn = "elec_price" if msn == "ESICD"
reshape long year, i(msn) j(yr)

drop state data_status msn
ren year elec_price
ren yr year

* deflate
merge m:1 year using `energy_dfltrs', assert(1 3) keep(3) nogen

* deflate to 2011 dollars
replace elec_price = elec_price * pien_avg
la var elec_price "avg elec prices, industrial sector, United States (2011 USD per million BTU)"

keep if year > 1975

tw (line elec_price year, lpattern(solid) lcolor(navy) msymbol(S)), ///
	graphr(color(white) lwidth(vthick)) yscale(r(14 24) noline) ylabel(14(2)24) xtit("Year") subtit("") title("") ///
	ytit("Price (2011 $ per Million BTU)") legend(off)
	
graph export "${results}\figures\elec_price.pdf", as(pdf) replace


*>------------- trends in shares ----------------------------------------------<

use "${dataSTATA}seds\seds_rawformatted.dta", clear
keep if state == "us"
keep if year > 1975

tw (line elecShareB_cl year, lpattern(longdash) lcolor(navy) msymbol(S)) (line elecShareB_ng year, lpattern(solid) lcolor(cranberry) msymbol(O)) ///
	(line elecShareB_pa year, lpattern(shortdash) lcolor(green) msymbol(D) mcolor(green)), ///
	graphr(color(white) lwidth(vthick)) yscale(noline) xtit("Year") subtit("") title("") ///
	ytit("Fuel Generation Share") legend(label(1 "Coal") label(2 "Natural Gas") label(3 "Oil") cols(3)) xlabel(1980(10)2010)
	
graph export "${results}\figures\fuel_shares.pdf", as(pdf) replace


*>------------------ data set for price map ---------------------------------------<

import delimited "${dataRAW}/user_provided_data/seds/pr_all.csv", varnames(1) clear 
keep if msn == "ESICD" & state != "US" // this is "electricity prices in the industrial sector, United States (dollars per million BTU)"

foreach var of varlist v* {
	local yr_lab : variable label `var'
	ren `var' year`yr_lab'
	}


replace msn = "elec_price" if msn == "ESICD"
reshape long year, i(state) j(yr)

drop data_status msn
ren year elec_price
ren yr year

drop if year > 2011

* deflate
merge m:1 year using `energy_dfltrs', assert(2 3) keep(3) nogen

* deflate to 2011 dollars
replace elec_price = elec_price * pien_avg
la var elec_price "avg elec prices, industrial sector, United States (2011 USD per million BTU)"

keep if year == 1976
drop pien_avg
ren state abbr


export delimited using "$dataR\state_price_data.csv", replace






